<template>
  <div>
    <div class="wolf-search">
      <input class="search-input" v-on:input="getRegionByKeyword" v-model="keyword" placeholder="请输入关键词..."/>
      <div class="search-suggestion" v-for="(region) of searchRegionList" :key="region.uid">
        <div class="suggestion-one">
          <div class="address-info" @click="localPositionByLocation(region)">
            <div class="name">{{ region.name }}</div>
            <div class="address">{{ region.address }}</div>
          </div>
          <i class="iconfont icon-dingwei suggestion-icon"></i>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {mapActions, mapState} from "vuex";

export default {
  name: "LocationSearch",
  data() {
    return {
      keyword: "",
    }
  },
  methods: {
    ...mapActions(["selectRegionByKeyword", "clearRegionByKeyword"]),
    getRegionByKeyword() {
      this.selectRegionByKeyword({
        query: this.keyword,
        tag: "",
        region: this.geolocation.geolocationInfo.address.city
      })
    },
    localPositionByLocation(region) {
      //清除关键词和列表
      this.keyword = region.name
      this.clearRegionByKeyword()
      // 用经纬度设置地图中心点
      this.map.clearOverlays();
      let new_point = new BMapGL.Point(region.location.lng, region.location.lat);
      // 创建标注
      let marker = new BMapGL.Marker(new_point);
      // 将标注添加到地图中
      this.map.addOverlay(marker);
      this.map.panTo(new_point);
    },
  },
  computed: {
    ...mapState(["searchRegionList", "map", "geolocation"])
  },
  destroyed() {
    this.map.clearOverlays();
    this.clearRegionByKeyword()
  }
}
</script>

<style scoped lang="less">
.wolf-search {
  width: 97%;
  height: auto;
  left: 50%;
  top: 15px;
  transform: translateX(-50%);
  position: fixed;
  z-index: 999;
  box-sizing: border-box;
  padding: 0 10px 0 10px;
  background-color: #fff;
  border-radius: 10px;
  font-size: 14px;

  .search-input {
    width: 100%;
    height: 45px;
    &::placeholder{
      font-size: 12px;
    }
  }

  .search-suggestion {
    transition: 1s height;

    .suggestion-one {
      width: 100%;
      height: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .address-info {
        padding: 5px 0;
        display: flex;
        align-content: center;
        flex-flow: column;
        font-size: 12px;
        width: 95%;

        .name {
          color: black;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          max-width: 85%;
        }

        .address {
          margin-top: 5px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          color: rgba(100, 100, 100);
          max-width: 85%;
        }
      }

    }
  }
}
</style>